package test;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dbBean.DBConnManager;

/**
 * Servlet implementation class AutoSave
 */
@WebServlet("/AutoSave")
public class AutoSave extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AutoSave() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	//PrintWriter out = response.getWriter();
    	String query = request.getQueryString();
    	
    	//String bhStr=(String)request.getSession().getAttribute("bianhao");
    	String xhStr=(String)request.getSession().getAttribute("xuehao");
    	int pandsl=(Integer)request.getSession().getAttribute("pandsl");   //转换成整型数
    	int danxsl=(Integer)request.getSession().getAttribute("danxsl");
    	int duoxsl=(Integer)request.getSession().getAttribute("duoxsl");
    	int pjd=pandsl+danxsl;           //判断题＋单选题（多选题起始的下标）
    	int zts=pjd+duoxsl;    //总题数
    	String ksdasz[]=new String[zts];
    	//保存判断题选择结果
    	if (pandsl>0) 
    	{
    		for(int i=0;i<pandsl;i++)	//判断题范围：0--4 题	
    		{
    			Matcher mPanduan = Pattern.compile("ksda\\[" + i + "\\]=(\\d+)").matcher(query);
    			if (mPanduan.find()) {
    				ksdasz[i] = mPanduan.group(1);
    			} else {
    				ksdasz[i] = "0000";
    			}
    		}
    	}
    	//保存单选题选择结果
    	if (danxsl>0)
    	{
    		for(int i=pandsl;i<pjd;i++)	//单选题范围	
    		{
    			Matcher mDanxuan = Pattern.compile("ksda2\\[" + i + "\\]=(\\d+)").matcher(query);
    			if (mDanxuan.find()) {
    				ksdasz[i] = mDanxuan.group(1);
    			} else {
    				ksdasz[i] = "0000";
    			}
    		}
    	}
    	//保存多选题选择结果（方法1）
    	if (duoxsl>0)
    	{
    		for(int i=pjd;i<zts;i++)	//多选题范围	
    		{
    			ksdasz[i] = "";
    			Matcher mDuoxuan = null;
    			
    			mDuoxuan = Pattern.compile("ksda3\\[" + i + "\\]\\[0\\]=(\\d+)").matcher(query);
    			if (mDuoxuan.find()) {ksdasz[i] += mDuoxuan.group(1);} else {ksdasz[i] += "0";}
    			mDuoxuan = Pattern.compile("ksda3\\[" + i + "\\]\\[1\\]=(\\d+)").matcher(query);
    			if (mDuoxuan.find()) {ksdasz[i] += mDuoxuan.group(1);} else {ksdasz[i] += "0";}
    			mDuoxuan = Pattern.compile("ksda3\\[" + i + "\\]\\[2\\]=(\\d+)").matcher(query);
    			if (mDuoxuan.find()) {ksdasz[i] += mDuoxuan.group(1);} else {ksdasz[i] += "0";}
    			mDuoxuan = Pattern.compile("ksda3\\[" + i + "\\]\\[3\\]=(\\d+)").matcher(query);
    			if (mDuoxuan.find()) {ksdasz[i] += mDuoxuan.group(1);} else {ksdasz[i] += "0";}
    			
    		}
    	}
    	
    	//把答案写入考生答案表中
    	DBConnManager connManager = new DBConnManager();
    	Connection conn = connManager.getConnection("mysql");
    	Statement stmt;
		try {
			stmt = conn.createStatement();
			for (int i = 0; i < zts; i++) {
				System.out.println(ksdasz[i]);
			}
			for(int i=0;i<zts;i++)
	    		//stmt.executeUpdate("update ksdab set stbh"+(i+1)+"='"+ksdasz[i]+"' where bh='"+bhStr+"'");
				stmt.executeUpdate("update ksdab set stbh"+(i+1)+"='"+ksdasz[i]+"' where xh='"+xhStr+"'");
	    	stmt.close();
	    	connManager.releaseConnection("mysql",conn);
	    	connManager.closeConns();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
		response.setHeader("Cache-Control", "no-cache");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
		response.setHeader("Cache-Control", "no-cache");
	}

}
